package com.example.wan1.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wan1.R;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.CollectArticleBean;
import com.example.wan1.model.MixedEntity;
import com.example.wan1.model.MixedHomeEntity;
import com.example.wan1.utils.GlideRoundedCornersTransform;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.ScreenUtils;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.widget.ZQImageViewRoundOval;

import java.util.List;

/***
 * 创建时间：2020/7/26 13:39
 * 创建人：10850
 * 功能描述：我的收藏 适配器，包含文章和项目两个部分
 */
public class MixedHomeAdapter extends BaseMultiItemQuickAdapter<MixedHomeEntity, BaseViewHolder> {

    public MixedHomeAdapter(List<MixedHomeEntity> data) {
        super(data);
        addItemType(MixedEntity.ART, R.layout.article_item);
        addItemType(MixedEntity.PRO, R.layout.project_list_item);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, MixedHomeEntity mixedEntity) {
        ArticleBean item = mixedEntity.getBean();
        switch (helper.getItemViewType()) {
            case MixedEntity.ART:
                //分类
                helper.setText(R.id.article_tv_category, item.getSuperChapterName() + " | " + item.getChapterName());
                //分享人
                if (!TextUtils.isEmpty(item.getShareUser()))
                    helper.setText(R.id.article_tv_author, item.getShareUser());
                else
                    helper.setText(R.id.article_tv_author, item.getAuthor());
                //时间+标题
                helper.setText(R.id.article_tv_time, item.getNiceDate());
                helper.setText(R.id.article_tv_title, Html.fromHtml(item.getTitle()).toString());
                //收藏
                if (item.isCollect()) {
                    helper.setImageResource(R.id.article_iv_collect, R.mipmap.heart_red);
                } else {
                    helper.setImageResource(R.id.article_iv_collect, R.mipmap.heart_gray);
                }
                //新
                if (item.isFresh()) {
                    helper.getView(R.id.article_tv_new).setVisibility(View.VISIBLE);
                } else
                    helper.getView(R.id.article_tv_new).setVisibility(View.GONE);

                //问答、公众号
                List<ArticleBean.TagsBean> tags = item.getTags();
                if (tags.size() != 0) {
                    if (tags.size() == 3) {
                        helper.getView(R.id.article_tv_question).setVisibility(View.VISIBLE);
                        helper.getView(R.id.article_tv_gzh).setVisibility(View.VISIBLE);
                        helper.getView(R.id.article_tv_web).setVisibility(View.VISIBLE);
                    } else {
                        for (int i = 0; i < tags.size(); i++) {
                            if ("问答".equals(tags.get(i).getName()))
                                helper.getView(R.id.article_tv_question).setVisibility(View.VISIBLE);
                            else if ("公众号".equals(tags.get(i).getName()))
                                helper.getView(R.id.article_tv_gzh).setVisibility(View.VISIBLE);
                            else if ("本站发布".equals(tags.get(i).getName()))
                                helper.getView(R.id.article_tv_web).setVisibility(View.VISIBLE);
                        }
                    }
                } else {
                    helper.getView(R.id.article_tv_question).setVisibility(View.GONE);
                    helper.getView(R.id.article_tv_gzh).setVisibility(View.GONE);
                    helper.getView(R.id.article_tv_web).setVisibility(View.GONE);
                }

                //置顶是另外一个url
                if (item.getType() == 1) {
                    helper.getView(R.id.article_tv_top).setVisibility(View.VISIBLE);
                } else
                    helper.getView(R.id.article_tv_top).setVisibility(View.GONE);
                helper.addOnClickListener(R.id.article_iv_collect);
                helper.addOnClickListener(R.id.article_tv_author);
                ConstraintLayout cl = helper.getView(R.id.article_cl);
                GradientDrawable background = (GradientDrawable) cl.getBackground();
                background.setStroke(2, ToolsUtils.getColor(mContext));
                //cl.setBackground(background);
                break;
            case MixedEntity.PRO:
                //增加圆角
                ZQImageViewRoundOval iv = helper.getView(R.id.project_list_iv);
                iv.setType(ZQImageViewRoundOval.TYPE_ROUND);
                //iv.setRoundRadius(16);

                //https://blog.csdn.net/MingJieZuo/article/details/83788735
                //使用主题颜色，当作placeholder
                TypedValue typedValue = new TypedValue();
                mContext.getTheme().resolveAttribute(R.attr.colorPrimary,
                        typedValue, true);
                int data = typedValue.data;
                ColorDrawable drawable1 = new ColorDrawable(data);

                String theme_color = (String) SPUtils.get(mContext, "theme_color", "");
                if (!TextUtils.isEmpty(theme_color)) {
                    drawable1.setColor(Color.parseColor(theme_color));
                }

                RequestOptions myOptions = new RequestOptions().optionalTransform
                        (new GlideRoundedCornersTransform(ScreenUtils.dip2px(mContext, 5f)
                                , GlideRoundedCornersTransform.CornerType.ALL))/*.placeholder(R.drawable.image_place_holder)*/
                        .placeholder(drawable1);
                Glide.with(mContext).load(item.getEnvelopePic()).apply(myOptions).into(iv);


                helper.setText(R.id.project_list_author, item.getAuthor())
                        .setText(R.id.project_list_title, item.getTitle())
                        .setText(R.id.project_list_desc, Html.fromHtml(item.getDesc()).toString())
                        .setText(R.id.project_list_time, item.getNiceDate());

                if (item.isCollect())
                    helper.setImageResource(R.id.project_list_collect, R.mipmap.heart_red);
                else
                    helper.setImageResource(R.id.project_list_collect, R.mipmap.heart_gray);

                helper.setGone(R.id.project_list_chaptername, true).setText(R.id.project_list_chaptername, item.getChapterName());

                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.user);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                TextView view = helper.getView(R.id.project_list_author);
                view.setCompoundDrawables(drawable, null, null, null);

                helper.addOnClickListener(R.id.project_list_chaptername)
                        .addOnClickListener(R.id.project_list_collect);
                break;
        }
    }
}
